<?php

defined('SYSPATH') or die('No direct script access.');

class Model_Orm_Images extends ORM {

	protected $_table_name = 'images';

	protected $_table_columns = array(
		'id' => NULL,
		'text' => NULL,
		'file' => NULL,
	);

	public static function item_save($array){
		$data = ORM::factory('orm_images');
		$data->text= $array['text'];
		$data->file= $array['file'];
		return $data->save();
	}


	public function generate_image(){
		if (!function_exists('gd_info')) {
			return FALSE;
		}
		$file_path = DOCROOT . Kohana::$config->load('names.picture.path') . $this->file;
		$helper = new Helper_Default();
		if ($this->file != '' AND file_exists($file_path)) {
			$path_new = Kohana::$config->load('names.picture.small.path') . $this->file;
			$file_path_new = DOCROOT . $path_new;
			if (!file_exists($file_path_new)) {
				$helper->create_image($file_path, $file_path_new, Kohana::$config->load('names.picture.small.width'), Kohana::$config->load('names.picture.small.height'));
			}
			return '/' . $path_new;
		}
		else {
			return FALSE;
		}
	}
}